﻿using Marss.JsonViewer.ViewModels.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using Xamasoft.JsonClassGenerator;
using Xamasoft.JsonClassGenerator.CodeWriters;

namespace Marss.JsonViewer.ViewModels
{
    public class JsonToCSTabViewModel : TabViewModelBase
    {
        public JsonToCSTabViewModel()
        {
            Header = "JSON转C#";

            GenerateCommand = new GenericCommand<JsonToCSTabViewModel, object>(this, GenerateCS, CanGenerate);
            SelectTargetFolderCommand = new RelayCommand(SelectFolder);
        }

        #region 继承
        public override bool CanBeRemoved
        {
            get
            {
                return false;
            }
        }

        public override bool Editable
        {
            get
            {
                return false;
            }
        }

        public override string UserControlName
        {
            get
            {
                return "JsonToCSControl";
            }
        }
        #endregion 继承

        #region 属性
        private string _untransformJson;
        public string UnTransformJson
        {
            get { return _untransformJson; }
            set { SetProperty(ref _untransformJson, value, "UnTransformJson"); }
        }

        private string _namespaceStr = "Example";
        public string NamespaceStr
        {
            get { return _namespaceStr; }
            set { SetProperty(ref _namespaceStr, value, "NamespaceStr"); }
        }

        private string _mainClassName = "SampleClass";
        public string MainClassName
        {
            get { return _mainClassName; }
            set { SetProperty(ref _mainClassName, value, "MainClassName"); }
        }

        private string _targetFolder;
        public string TargetFolder
        {
            get { return _targetFolder; }
            set { SetProperty(ref _targetFolder, value, "TargetFolder"); }
        }

        private string _secondaryNamespace = "Example.JsonTypes";
        public string SecondaryNamespace
        {
            get { return _secondaryNamespace; }
            set { SetProperty(ref _secondaryNamespace, value, "SecondaryNamespace"); }
        }

        private bool _useProperties = true;
        public bool UseProperties
        {
            get { return _useProperties; }
            set { SetProperty(ref _useProperties, value, "UseProperties"); }
        }

        private bool _internalVisibility = false;
        public bool InternalVisibility
        {
            get { return _internalVisibility; }
            set { SetProperty(ref _internalVisibility, value, "InternalVisibility"); }
        }

        private bool _useNestedClasses;
        public bool UseNestedClasses
        {
            get { return _useNestedClasses; }
            set { SetProperty(ref _useNestedClasses, value, "UseNestedClasses"); }
        }

        private bool _useSecondaryNamespace;
        public bool UseSecondaryNamespace
        {
            get { return _useSecondaryNamespace; }
            set { SetProperty(ref _useSecondaryNamespace, value, "UseSecondaryNamespace"); }
        }

        private bool _useSingleFile;
        public bool UseSingleFile
        {
            get { return _useSingleFile; }
            set { SetProperty(ref _useSingleFile, value, "UseSingleFile"); }
        }

        private bool _usePascalCase;
        public bool UsePascalCase
        {
            get { return _usePascalCase; }
            set { SetProperty(ref _usePascalCase, value, "UsePascalCase"); }
        }

        private string _superClass;
        public string SuperClass
        {
            get { return _superClass; }
            set { SetProperty(ref _superClass, value, "SuperClass"); }
        }
        #endregion 属性

        #region 命令
        public ICommand GenerateCommand { get; private set; }

        public ICommand SelectTargetFolderCommand { get; private set; }
        #endregion 命令

        #region 私有方法

        private void GenerateCS(JsonToCSTabViewModel vm, object parameter)
        {
            try
            {
                var gen = Prepare();
                gen.GenerateClasses();
                MessageBox.Show("生成完成！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool CanGenerate(JsonToCSTabViewModel vm, object parameter)
        {
            //TODO：检查各个条件是否合适
            if (string.IsNullOrEmpty(UnTransformJson))//如果内容为空就不能转换
                return false;
            if (string.IsNullOrEmpty(NamespaceStr))
                return false;
            if (string.IsNullOrEmpty(TargetFolder))
                return false;
            return true;
        }

        private void SelectFolder(object parameter)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                TargetFolder = folderBrowser.SelectedPath;
            }
        }

        private JsonClassGenerator Prepare()
        {
            var gen = new JsonClassGenerator();
            gen.Example = UnTransformJson;
            gen.InternalVisibility = InternalVisibility;
            gen.CodeWriter = new CSharpCodeWriter();
            gen.ExplicitDeserialization = false;
            gen.Namespace = string.IsNullOrEmpty(NamespaceStr) ? null : NamespaceStr;
            gen.NoHelperClass = false;
            gen.SecondaryNamespace = UseSecondaryNamespace && !string.IsNullOrEmpty(SecondaryNamespace) ? SecondaryNamespace : null;
            gen.TargetFolder = TargetFolder;
            gen.UseProperties = UseProperties;
            gen.MainClass = MainClassName;
            gen.UsePascalCase = UsePascalCase;
            gen.UseNestedClasses = UseNestedClasses;
            gen.ApplyObfuscationAttributes = false;
            gen.SingleFile = UseSingleFile;
            gen.ExamplesInDocumentation = true;
            gen.SuperClass = SuperClass;
            return gen;
        }
        #endregion 私有方法
    }
}
